import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import {createSvgIconsPlugin} from "vite-plugin-svg-icons";
// mock
import {viteMockServe} from "vite-plugin-mock";
// https://vitejs.dev/config/
export default defineConfig(({command, mode}) => {
    // mode即为加载的.env文件，cwd为index.html所在根目录，即为.env文件所在位置
    const env = loadEnv(mode, process.cwd())
    return {
        plugins: [
            vue(),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                symbolId: 'icon-[dir]-[name]'
            }),
            viteMockServe({
                mockPath:'mock',
                enable: command === 'serve' //开发阶段可使用mock
            })
        ],
        resolve: {
            alias: {
                "@": path.resolve("./src")
            }
        },
        // scss全局变量的配置
        css: {
            preprocessorOptions: {
                scss: {
                    javascriptEnabled: true,
                    additionalData: '@import "./src/styles/variable.scss";'
                }
            }
        },
        server:{
            proxy:{
                [env.VITE_APP_BASE_API]: {
                    target:env.VITE_SERVE,
                    changeOrigin:true,
                    rewrite:(path) => path.replace(/^\/api/, '')
                }
            }
        }
    }
})
